package com.fansea.util;//java环境中文传值时，需特别注意字符编码问题

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.net.*;
import java.io.*;

public class DemoTianAPI {


    public static String getMsg(String msg) throws Exception {
        String tianapi_data = "";
        try {
            URL url = new URL("https://apis.tianapi.com/robot/index");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            OutputStream outputStream = conn.getOutputStream();
            String content = "key=f6879bdc860b7f4bab301e4a22222260&question="+msg;
            outputStream.write(content.getBytes());
            outputStream.flush();
            outputStream.close();
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder tianapi = new StringBuilder();
            String temp = null;
            while (null != (temp = bufferedReader.readLine())) {
                tianapi.append(temp);
            }
            tianapi_data = tianapi.toString();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = JSONStringHandleUtil.getStats(tianapi_data, "result");
        String reply = JSONStringHandleUtil.getStats(result, "reply");

        return reply;
    }

    public static String getNews() throws Exception {
        String tianapi_data = "";
        try {
            URL url = new URL( "https://apis.tianapi.com/bulletin/index");
            HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            OutputStream outputStream = conn.getOutputStream();
            String content = "key=f6879bdc860b7f4bab301e4a22222260";
            outputStream.write(content.getBytes());
            outputStream.flush();
            outputStream.close();
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader (inputStream,"utf-8");
            BufferedReader bufferedReader = new BufferedReader (inputStreamReader);
            StringBuilder tianapi = new StringBuilder();
            String temp = null;
            while ( null != (temp = bufferedReader.readLine())){
                tianapi.append(temp);
            }
            tianapi_data = tianapi.toString();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = JSONStringHandleUtil.getStats(tianapi_data, "result");
        String reply = JSONStringHandleUtil.getStats(result, "list");
        return    reply;


    }


    public static String getMsg2(String msg ){
        try {
            String s = HttpRequestUtil.doGet("http://api.qingyunke.com/api.php?key=free&appid=0&msg="+URLEncoder.encode(msg, "UTF-8"));
            String content = JSONStringHandleUtil.getStats(s, "content");
            return content;
        } catch (UnsupportedEncodingException e) {
            return "机器人吃饭中";
        }
    }

    //java环境中文传值时，需特别注意字符编码问题

        public static String getJoker() throws Exception {
            String tianapi_data = "";
            try {
                URL url = new URL( "https://apis.tianapi.com/joke/index");
                HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                conn.setDoOutput(true);
                conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                OutputStream outputStream = conn.getOutputStream();
                String content = "key=f6879bdc860b7f4bab301e4a22222260&num=1";
                outputStream.write(content.getBytes());
                outputStream.flush();
                outputStream.close();
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader (inputStream,"utf-8");
                BufferedReader bufferedReader = new BufferedReader (inputStreamReader);
                StringBuilder tianapi = new StringBuilder();
                String temp = null;
                while ( null != (temp = bufferedReader.readLine())){
                    tianapi.append(temp);
                }
                tianapi_data = tianapi.toString();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(tianapi_data);
            String result = JSONStringHandleUtil.getStats(tianapi_data, "result");
            JSONObject parse =  JSONObject.parseObject(result);
            JSONArray list = parse.getJSONArray("list");
            JSONObject jsonObject= list.getJSONObject(0);
            String  content= jsonObject.getString("content");
            return    content;
        }
}